<!doctype html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security" xmlns:auth=""
      class="x-admin-sm">
<auth:data uri="/system/role/list.json"/>
<head>
    <th:block th:include="common/head::noXadmin('分配角色')"/>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row ">
        <div class="layui-col-md12">
            <div class="layui-card" id="queryBox">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="roleName" placeholder="请输入角色名称" autocomplete="off"
                                   class="layui-input"></div>
                        <div class="layui-inline layui-show-xs-block">
                            <select name="status" lay-verify="">
                                <option value="">状态</option>
                                <option value="0"
                                        th:each="status:${T(com.hardydou.common.constant.Const.Status).values()}"
                                        th:value="${status.val}"
                                        th:text="${status.name}">
                                </option>
                            </select>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit="" lay-filter="sreach">
                                <i class="layui-icon">&#xe615;</i></button>
                        </div>
                    </form>
                </div>
            </div>
            <div class="layui-card">
                <div class="split-line-y split-line-y-open">
                    <i class="fa "></i>
                </div>
                <div class="layui-card-body ">
                    <table class="layui-table" id="dataList" lay-filter="data"></table>
                </div>
                <div class="text-align-center">
                    <button class="layui-btn" id="save">保存权限</button>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script th:inline="javascript">
    const uId=[[${param.userId[0]}]];
    const roleIds = [[${roles}]];
</script>
<th:block th:inline="none">

    <script type="text/html" id="statusTpl">
        <!-- 这里的checked的状态只是演示 -->
        <input type="checkbox" name="status" value="{{d.roleId}}" lay-skin="switch" lay-text="启用|停用"
               {{d.status==0?"checked":""}} lay-filter="status" disabled="disabled">
    </script>
    <script>
        layui.use(['jquery'], function () {
            var $ = layui.jquery;
            var queryBoxShow = true;
            $(".split-line-y >i").click(function () {
                if (queryBoxShow) {
                    queryBoxShow = false;
                    $("#queryBox").slideUp('fast');
                    $(".split-line-y").removeClass('split-line-y-open').addClass('split-line-y-closed');
                } else {
                    queryBoxShow = true;
                    $("#queryBox").slideDown('fast');
                    $(".split-line-y").removeClass('split-line-y-closed').addClass('split-line-y-open');
                }

            });
        })
        layui.use(['table', 'form'],
            function () {
                var table = layui.table,
                    form = layui.form;
                table.render({
                    elem: '#dataList'
                    , totalRow: false
                    , url: '/system/role/list.json' //数据接口
                    , page: true //开启分页
                    , response: {
                        statusCode: 0
                    }
                    , cols: [[
                        {checkbox: true, type: 'checkbox', fixed: 'left'}
                        , {field: 'roleId', title: 'ID', sort: true, fixed: 'left'}
                        , {field: 'roleName', title: '角色名称',}
                        , {field: 'roleKey', title: '角色标识',}
                        , {field: 'status', title: '状态', templet: '#statusTpl'}
                        , {field: 'ctime', title: '创建时间', width: 180}
                    ]],
                    parseData: function (res) {
                        return {
                            code: res.code,
                            msg: res.message,
                            count: res.data.total,
                            data: res.data.list.map((d) => {
                                roleIds[d.roleId] && (d['LAY_CHECKED'] = true);
                                return d
                            }),
                            totalRow: {
                                score: "score",
                                experience: "exp"
                            }
                        }

                    }
                    , request: {
                        pageName: 'pageNum' //页码的参数名称，默认：page
                        , limitName: 'pageSize' //每页数据量的参数名，默认：limit
                    }
                })
                form.on('submit(sreach)', function (data) {
                    table.reload('dataList', {
                            page: {
                                curr: 1
                            },
                            where: data.field
                        }
                    )
                    return false;
                })

                table.on('checkbox(data)', function (obj) {
                    table.cache['dataList'].map(x => {
                        delete roleIds[x.roleId];
                    });
                    let checkStatus = table.checkStatus('dataList');
                    checkStatus.data.map(x => {
                        roleIds[x.roleId] = true;
                    })
                });

                $("#save").on('click', function () {
                    let rIds=[];
                    for (r in roleIds){
                        rIds.push(r);
                    }
                    $.ajax({
                        url: rootPath + 'system/user/auth',
                        data: {
                            userId: uId,
                            roleIdsStr: rIds.join(",")
                        },
                        type: "PUT",
                        dataType: "json",
                        success: function (data) {
                            layer.msg(data.message);
                            layer.close(index);
                        },
                        error: function (data) {
                            layer.msg(data.message);
                        }
                    });
                })
            });</script>
</th:block>
</html>